Paint Simulator
Volume Number: 1
Issue Number: 13
Column Tag: Special Projects
A Mac Paint Simulator in Basic 
By Mike Steiner, Sierra Vista, AZ, MacTutor Contributing Editor
MacPaint Functions in Basic!
The program, written in Microsoft BASIC, accompanying this column simulates
some of the features of Macpaint. “Why,” you are probably asking yourself, “would
anyone want to simulate Macpaint in a BASIC program when every Macintosh comes
with Macpaint at no extra cost?” The reason is that transferring a Macpaint picture to
a BASIC program is not the easiest thing in the world to do. Having your own Basic
version of MacPaint can be very helpful. [The real reason is the challenge! And Mike
has succeeded admirably. -Ed]
Here are some of the problems of trying to mix picts with Basic: After making the
picture, you have to copy it to the clipboard (which limits you to the size of the
Macpaint window, thus eliminating the ability to make a full-screen picture) before
entering BASIC. Once in BASIC, you then have to transfer the picture from the
clipboard to the screen. If you want to use the picture again, you have to save it as a
disk file. The program with this article will let you draw a picture, save it to and load
it from a disk. The routine that loads pictures from disk can be incorporated into your
own programs to let you convientiently use pictures in them.
Program Notes
The program is fairly straight forward and does not use any programming
“tricks;” everything used is documented in the Microsoft BASIC manual. However, it
requires a 512K Macintosh . A 128K Mac does not have enough memory to handle the
necessary arrays and heap requirements.
Instructions are incorporated into the program and are accessable via the
Instructions option in the Options menu. There is one area where operation of the
program differs from the standard and bears mentioning here. When you save a
picture to disk, you are not limited to saving the entire picture. You must select the
section of the picture (to include the whole picture if you want) as the first step in the
save process.
Fig. 1 Screen of Mac Paint Simulator
How it Works
The program starts by CLEARing memory from the data segment to increase the
application heap memory. The heap is where BUTTON, EDIT FIELD, and PICTURE
information are stored. Without the increase of heap memory the program results in a
system error 25 (out of heap space) whenever it tries to print a picture.
The program then performs a GOSUB to init, which is the initialization routine.
This routine causes all variables to default to integers unless they are declared
otherwise. (The only other variable format used is string variables.) The program
then DIMensions the arrays it will use and generates the drawing window and the
program menus. It then dispays the names of the program and programmer and the
version and date. (This display is also available by selecting “About...” from the File
menu.) It then initializes variables and the cursor arrays and RETURNs to the main
program.
The routine called start is the main routine of the program. All subroutines
eventually return to start. While in the start loop, the program enables drawing and
checks for a menu selection. The loop sends the program to the subroutine
cursorposition. This subroutine monitors the position of the cursor on the screen and
changes the cursor to an arrow when it is outside the drawing window and changes it to
a cross when inside the window. Note the statement “dummy = MOUSE (0)” preceding
the GOSUB. References to MOUSE (1) through MOUSE (6) return values related to the
most recent click of the mouse button. MOUSE (0) checks for a mouse click, so even
though it is not needed by the cursorposition routine, it must have been referenced for
the routine to work. The ‘ON shape GOSUB’ line sends the program to the appropriate
drawing routine, depending on what shape has been selected to be drawn.
Drawing Shapes
The routines box, roundbox, and oval are essentially the same routine, differing
only in the Quickdraw routines they use. They first check to see if the mouse button
has been pressed. If it has not, the routines RETURN to start. If the button has been
pressed, the routine enters a loop that first goes to a subroutine called getpoints.
Getpoints determines the coordinates of diagonally opposite corners of a rectangle. It
assigns the top left corner as the start and the bottom right corner as the end, putting
the coordinates in the variable array “rect.” Control is then returned to the
shape-drawing routine where the shape is drawn twice and getpoints is entered again.
Because the penmode was set to 10 (XOR) before the routine was entered, the pen
reverses the color of the screen it passes over; by drawing the shape twice, the shape
continually draws and erases itself. When the mouse button is released, control is
returned to the drawing program where the pen mode is set to 8 (copy) and the shape
drawn once again to leave it on the screen. The routine then RETURNs to start.
The straightline routine is similar to the framed shape routines except that
starting and ending points are not swapped to insure that start is to the upper-left of
finish, and a straight line is drawn. The subroutine called free is the simplest of all.
It continually draws a line to the current cursor position from the previous cursor
position after moving initially to the point where the mouse button was pressed.
Text first sets the text mode to 1 (OR). A bug in the quickdraw routine that
draws text puts blank spaces after the text that is printed. These spaces will erase any
black pixels that are close to the end of the text. Setting the text mode to OR eliminates
this problem. The routine then sets the selected text size. The first time the routine is
entered after it is selected, when the mouse button is pressed, the left margin is set at
the cursor's horizontal position. Text is entered normally, and when the Return key is
pressed, text continues on the next line at the left margin. You may change fonts, size,
and styles of the text while entering it without losing your character's position on the